<?php
include '../../auth/ceklogin.php';
include '../../sql/conn.php';
$user=$_SESSION['userbkknt'];
if(isset($_REQUEST['step'])){
    $step=$_REQUEST['step'];
    if($step=='1'){
        $noInquiry=$_REQUEST['no_inquiry'];
        //cek apakah inquiry sudah ada yg memproses ato belum
        $query_step1_1=mysql_query("select quotation.no_inquiry from quotation where no_inquiry='$noInquiry'");
        if(mysql_fetch_array($query_step1_1)){
            echo json_encode(array('msg'=>'No inquiry tersebut sudah ada yang memproses'));
        } else {
            //cek idQuotation terakhir
            $query_step1_2=mysql_query("SELECT idQuotation FROM quotation ORDER BY idQuotation DESC LIMIT 0,1");
            $idquotation=mysql_fetch_array($query_step1_2);
            $idquotation_new=$idquotation[0]+1; //tambah satu untuk data baru
            
            //cek noQuotation temporary terahir
            $query_step1_3=mysql_query("select noQuotation from quotation where noQuotation like 'temp%' order by noQuotation DESC limit 0,1");
            $result_step1_3=mysql_fetch_array($query_step1_3);
            $hasil_step1_3=explode('temp',$result_step1_3[0]);
            $temp=$hasil_step1_3[1]+1;
            $noQuotation_new="temp".$temp; //tambah satu untuk data baru

            //create new temporary quotation
            $query_step1_3=mysql_query("
                    INSERT INTO quotation
                    (`idQuotation`,`noQuotation`,`no_inquiry`,`crtbyQuotation`) 
                    VALUES
                    ('$idquotation_new','$noQuotation_new','$noInquiry','$user')
                    ");
            if($query_step1_3){
                $query_step1_4=mysql_query("UPDATE inquiry SET status='USED' WHERE no_inquiry='$noInquiry'");
                if($query_step1_4){
                    $_SESSION['usedinquiry']=$noInquiry;
                    echo json_encode(array('success'=>true,'msg'=>$idquotation_new));
                } else{
                    echo json_encode(array('msg'=>mysql_error()));
                }
            } else{
                echo json_encode(array('msg'=>mysql_error()));
            }
        }
    }
    elseif($step=='finish'){
    }
}
if(isset($_REQUEST['cancel'])){
    if($_REQUEST['cancel']=='next2'){
        //cancel waktu membuat new quotation
        if(isset($_SESSION['usedinquiry'])){
            $noInquiry=$_SESSION['usedinquiry'];
            $query_cancel_0=mysql_query("SELECT idQuotation FROM `bukakainti`.`quotation` WHERE `quotation`.`no_inquiry` = '$noInquiry' LIMIT 1");
            $idQuotation=mysql_fetch_array($query_cancel_0);
            $idQuotation=$idQuotation[0];
            $query_cancel_0=mysql_query("DELETE FROM `bukakainti`.`quotation_item` WHERE CONCAT(`quotation_item`.`idQuotation`) = '$idQuotation'");
            $query_cancel_1=mysql_query("DELETE FROM `bukakainti`.`quotation` WHERE CONCAT(`quotation`.`no_inquiry`) = '$noInquiry' LIMIT 1");
            $query_cancel_2=mysql_query("UPDATE inquiry SET status='OSIN' WHERE no_inquiry='$noInquiry'");
            if($query_cancel_0 && $query_cancel_1 && $query_cancel_2){
                $_SESSION['usedinquiry']='';
                echo json_encode(array('success'=>true));
            } else{
                echo json_encode(array('msg'=>'Query Error.'));
            }
        } else {
            echo json_encode(array('msg'=>'Error session not set'));
        }
    }
    //cancel waktu edit quotation
    elseif($_REQUEST['cancel']=='nextedit2'){
        $idQuotation=$_REQUEST['idquotation'];
        $query_cancel_edit1=mysql_query("DELETE FROM `bukakainti`.`quotation_item` where `idQuotation`='$idQuotation' and priceItem like 'temp%'");
        if($query_cancel_edit1){
            echo json_encode(array('success'=>true));
        } else {
            echo json_encode(array('msg'=>'Query Error.'));
        }
    }

}
?>
